import { Component, OnInit } from '@angular/core';
import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { JhiEventManager } from 'ng-jhipster';

import { Account, LoginModalService, Principal } from '../shared';
import {BookTypeService} from "../entities/book-type/book-type.service";
import {BookType} from "../entities/book-type/book-type.model";
import {BookBasicService} from "../entities/book-basic/book-basic.service";
import {BookBasic} from "../entities/book-basic/book-basic.model";
import {ShoppingCartService} from "../entities/shopping-cart/shopping-cart.service";

@Component({
    selector: 'jhi-home',
    templateUrl: './home.component.html',
    styleUrls: [
        'home.css'/*,
        `[nz-carousel-content] {
      text-align: center;
      height: 160px;
      line-height: 160px;
      background: #364d79;
      color: #fff;
      overflow: hidden;
    }

    h3 {
      color: #fff;
    }
    `*/
    ]

})
export class HomeComponent implements OnInit {
    account: Account;
    modalRef: NgbModalRef;
    // 书类型数组
    bookType: Array<BookType> = new Array();

    bookBasic: Array<BookBasic>=new Array();

    currentBookType: BookType;
    currentKeyword: string = '';

    /*array = [ 1, 2, 3, 4 ];*/
    constructor(
        private principal: Principal,
        private loginModalService: LoginModalService,
        private eventManager: JhiEventManager,
        private bookTypeService: BookTypeService,
        private bookBasicService: BookBasicService,
        private shoppingCartService: ShoppingCartService
    ) {
    }

    ngOnInit() {
        this.principal.identity().then((account) => {
            this.account = account;
        });
        this.registerAuthenticationSuccess();
        this.loadbooktypes();//调用方法
        this.findBooksByKeyword();

        this.eventManager.subscribe('authenticationSuccess', (message) => {
            console.log('我接受了广播', message);
            console.log('我接受了广播', message.content);
            let keyword = message.content;
            if ( !keyword ) {
                keyword = '';
            }
            this.currentKeyword = keyword;
            this.findBooksByKeyword();
        });
    }
    // 加载书的类型
    loadbooktypes(){
        this.bookTypeService.getAllBookType(0, 100).subscribe(
            (data) => {
                console.log('bookTypes', data);
                this.bookType = data;
            },
            (error) => {console.log(error)}
        );
    }

    // 根据类型获取书
    findBooksByType(bookType: BookType) {
        console.log('根据书的类型获取书', bookType);
        if (this.currentBookType === bookType){
            console.log('重复点击，不进行处理');
            return;
        }
        this.currentBookType = bookType;
        this.findBooksByKeyword();


    }
    // 模糊查询取书
    findBooksByKeyword() {
        let type: number;
        const keyword: string = this.currentKeyword;

        if (this.currentBookType != null) {
            type = this.currentBookType.id;
        }else{
            type=0;
        }

        this.bookBasicService.findBooksByKeyword(type,keyword).subscribe(
            (data) => {
                console.log('keyword', data);
                console.log('dddddddddddd', data.content);
                if (data && data.content.length > 0) {
                    this.bookBasic = data.content;
                    console.log('sdfadfasdfasdfad', this.bookBasic);
                } else {
                    this.bookBasic = new Array<BookBasic>();
                }
            },
            (error) => {
                console.log(error)
            }
        );
    }

    //加入购物车
    /*saveBook(item : BookBasic){
        this.shoppingCartService.create(item).subscribe(
            (data)=>{
                console.log(data);
            }
        );
    }*/

    registerAuthenticationSuccess() {
        this.eventManager.subscribe('authenticationSuccess', (message) => {
            this.principal.identity().then((account) => {
                this.account = account;
            });
        });
    }

    isAuthenticated() {
        return this.principal.isAuthenticated();
    }

    login() {
        this.modalRef = this.loginModalService.open();
    }
}
